package com.cx.base.day13_io.io_03_bufferStream;

import java.io.*;

/**
 * @Author: robert.song
 * @Date: Created in 2022/4/22
 */
public class BufferedReader2Writer {

    public static void main(String[] args) throws IOException {
        baseStringBuffer();
    }

    public static void baseStringBuffer() throws IOException {
        File file1 = new File("." + properties() + "test0.txt");
        File file2 = new File("." + properties() + "test6.txt");
//        如果文件没有上级目录，获取父级目录，然后执行mkdirs()
        file1.getParentFile().mkdirs();
        file2.getParentFile().mkdirs();
        file1.createNewFile();
        file2.createNewFile();
        FileReader fr = new FileReader(file1);
        FileWriter fw = new FileWriter(file2, true);
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);
        String line = null;
        try {

            while ((line = br.readLine()) != null) {
                //          底层默认设置了8m的缓存，缓存满了刷新磁盘，
                //          缓冲流可在创建时自定义缓冲区大小，属于用户态的缓冲区，用数组实现
                //          文件流的底层是系统态缓冲区，通过Buffer来获取
                bw.write(line);
//              换行：newLine()方法，或者bw.write(line+"\n")
                bw.newLine();
                //            很明显这种情况是大数据量的流，不推荐再频繁执行flush()刷新磁盘
                //            bos.flush();
            }
        } finally {
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
        }
    }

    public static String properties() throws IOException {
        return System.getProperty("file.separator");
    }
}
